<template>
  <div class="room_chat">
    <div id="room">
      <div class="clear"></div>
      <!-- <infomy :list="mylist" /> -->
      <info :list="list" />
      <!-- <infomy :list="mylist" /> -->
    </div>
    <div class="footer_edit">
      <div class="footer_left">
        <img
          class="footer_img"
          src="https://www.hualigs.cn/image/6040ea1c3b41f.jpg"
          alt=""
        />
      </div>
      <div class="footer_center">
        <input
          v-model="infomation"
          type="text"
          class="footer_center_edit"
          @keyup.enter="submit"
        />
      </div>
      <div class="footer_right">
        <img
          class="footer_img"
          src="https://www.hualigs.cn/image/6040ea1c3bd9f.jpg"
          alt=""
        />
      </div>
    </div>
  </div>
</template>
<script
  src="https://code.jquery.com/jquery-1.11.1.js"
  rel="external nofollow"
></script>
<script>
// import Vue from "vue";
import info from "../../../components/chatroom/info";
import http from "../../../API/http";
export default {
  data() {
    return {
      msg: null,
      infomation: "",
      id: "",
      list: [],
    };
  },
  sockets: {
    connecting() {
      console.log("连接上");
    },
    sevchatRoom(data) {
      // console.log(data);
      data.userid === this.$store.state.ID ? (data.tag = 1) : (data.tag = 0);
      this.list.push(data);
    },
    receiveMsg(data) {
      let datas = this.$store.state.userinfo.userList;
      var flag = true;
      if (datas.length === 0) {
        this.$store.commit("setuserList", data);
      } else {
        for (let i = 0; i < datas.length; i++) {
          if (datas[i].userid === data.userid) {
            datas[i].content = data.content;
            flag = false;
            break;
          }
        }
        if (flag === true) {
          this.$store.commit("setuserList", data);
          // console.log("第一次发");
        }
      }
    },
  },
  async created() {
    let data = await http._chatRoom();
    let datas = data.data.data;
    datas.map((item) => {
     item.name === this.$store.state.Authorization ? (item.tag = 1) : (item.tag = 0);
    });
    this.list = datas;
    // console.log(this.$socket.id, "---全局获取---");
    // 全局获取并进行设置
    this.$store.commit("setsocketID", this.$socket.id);
  },
  beforeDestroy() {
    // this.$socket.close()
  },

  beforeUpdate() {
    // console.log(this.$socket.id,"beforeUpdate---");
    this.$store.commit("setsocketID", this.$socket.id);
  },
  components: {
    info,
  },

  methods: {
    submit() {
      if (this.infomation !== null) {
        let obj = {
          name: this.$store.state.Authorization,
          img: "https://www.hualigs.cn/image/60364a03036ce.jpg",
          tag: 0,
          content: this.infomation,
          id: this.$store.state.socketID,
          userid : this.$store.state.ID,
        };
        // this.list.push(obj);
        this.$socket.emit("chatRoom", obj);
      }
      this.infomation = "";
    },
  },
  watch: {},
};
</script>
<style scoped>
.room_chat {
  width: 100%;
  height: 100%;
  margin-bottom: 70px;
  background-color: #f3f3f3;
}
.room {
  position: relative;

  background-color: #f3f3f3;
}
.clear {
  clear: both;
}
.footer_edit {
  height: 7vh;
  width: 100%;
  position: fixed;
  bottom: 0;
  right: 0;
  left: 0;
  background-color: #ffffff;
  border-top: 1px solid #d8d8d8;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 999;
}
.footer_center {
  width: 74%;
  background-color: #f3f3f3;
  height: 5vh;
}
.footer_img {
  height: 5vh;
}
.footer_center_edit {
  width: 100%;
  background-color: #f3f3f3;
  height: 5vh;
  border: none;
  padding: 0 10px 0 10px;
  outline-style: none;
}
</style>
